package com.example.controller;

import com.example.constants.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("test")
public class TestController {
    /**
     * 注入 RabbitTemplate 模板对象
     */
    @Resource
    private RabbitTemplate rabbitTemplate;


    @RequestMapping("test/{name}")
    public String send(@PathVariable("name") String name) {

        /**
         * 发送消息
         */
        for (int i = 0; i < 100; i++) {
            String str = name + " " + i + " " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss_SSS").format(new Date());
            rabbitTemplate.convertAndSend(Constants.MY_FANOUT_EXCHANGE, "", str);
            System.out.println("消息发送成功!\t" + str);
        }


        return "success";
    }
}
